
<template>
  <div class="receive">
    <tab :line-width="2" active-color="rgb(80, 119, 185)" ref="tab" custom-bar-width="60px">
      <tab-item  ref="tabitem1" selected @on-item-click="handler">借出接收</tab-item>
      <tab-item  ref="tabitem2" @on-item-click="handler">转移接收</tab-item>
      <tab-item  ref="tabitem3" @on-item-click="handler" v-if="$store.state.user.approver.userId == $store.state.user.userInfo.wxUserId">转移审核</tab-item>
    </tab>
    <loading :show="isLoading"></loading>
    <div class="list">
      <template v-if="list.length > 0 ">
        <div v-if="active == 0">
          <orderBox v-for="(item, index) in list" :key="index" :item="item" @getList= "handleGetList"></orderBox>
        </div>
        <div v-if="active == 1">
          <TransferBox v-for="(item, index) in list" :key="index" :item="item" receiveBtn @getList= "handleGetList"></TransferBox>
        </div>
        <div v-if="active == 2">
          <TransferBox v-for="(item, index) in list" :key="index" :item="item" approverBtn @getList= "handleGetList"></TransferBox>
        </div>
      </template>
      <template v-else>
        <no-data></no-data>
      </template>
    </div>
  </div>
</template>

<script>
import { XButton, Tab, TabItem, Timeline, TimelineItem, CheckIcon, Icon, Loading  } from "vux";
import AssetApi from "@/api/asset.js";
import orderBox from "../components/order-box.vue";
import TransferBox from "../components/Transfer-box.vue";
import { set } from 'vue';
export default {
  components: {
    XButton,
    Tab,
    TabItem,
    Timeline,
    TimelineItem,
    CheckIcon,
    Icon,
    Loading,
    orderBox,
    TransferBox,
  },
  data() {
    return {
      list: [],
      active:0,
      isLoading:false,
    };
  },
  mounted() {
    let receiveObj = this.$route.query
    if(receiveObj.order == 0 && receiveObj.transfer != 0){
      setTimeout(()=>{
        this.$refs.tabitem2.onItemClick(1)
      })
    }else{
      // setTimeout(()=>{
      //   this.$refs.tabitem1.onItemClick(0)
      // })
      this.getLendReceiveList()
    }
  },
  methods: {
    // 根据当前active值刷新列表
    handleGetList(){
      let active = this.active
      if (active == 0) {
        this.getLendReceiveList();
      } 
      if (active == 1) {
        this.getTransferReceiveList();
      } 
      if (active == 2) {
        this.findApproverAllList();
      } 
    },
    handler(index) {
      if(this.active == index)return
      this.list = []
      this.active = index;
      if (index == 0) {
        this.getLendReceiveList();
      } 
      if (index == 1) {
        this.getTransferReceiveList();
      } 
      if (index == 2) {
        this.findApproverAllList();
      } 
    },
    // 获取借出接收
    getLendReceiveList() {
      this.isLoading = true
      let _params = {
        page: 1,
        pageSize: 1000,
      };
      AssetApi.findUnconfirmBorrowAll(_params).then((res) => {
        this.isLoading = false
        if (res.code == "0000") {
          this.list = res.data.records || []
        }
      });
    },
    // 获取审批列表
    findApproverAllList() {
      this.isLoading = true
      let _params = {
        page: 1,
        pageSize: 1000,
        userId:this.$store.state.user.userInfo.wxUserId
      };
      AssetApi.findApproverAll(_params).then((res) => {
        this.isLoading = false
        if (res.code == "0000") {
          this.list = res.data.records || []
        }
      });
    },
    // 获取转移接收
    getTransferReceiveList() {
      this.isLoading = true
      let _params = {
        page: 1,
        pageSize: 1000,
      };
      AssetApi.receiveTransfer(_params).then((res) => {
        this.isLoading = false
        if (res.code == "0000") {
          let list = res.data.records || []
          let arr = []
          list.forEach(element => {
            if(element.spStatus)arr.push(element)
          });
          this.list = list
        }
      });
    },
    confirmReceive(item) {
      let _this = this
      _this.$vux.confirm.show({
        title: "确认接收",
        content: "确认接收后不可更改",
        onCancel() {},
        onConfirm() {
          _this.isLoading = true
          if (_this.active == 0) {
            AssetApi.orderConfirm({ orderId: item.orderId }).then((res) => {
              _this.isLoading = false
              if (res.code == "0000") {
                _this.$vux.toast.show({
                  text: "资产接收确认成功",
                });
                _this.getLendReceiveList()
              }
            });
          } else {
            AssetApi.transferConfirm({ id: item.id }).then((res) => {
              _this.isLoading = false
              if (res.code == "0000") {
                _this.$vux.toast.show({
                  text: "资产接收确认成功",
                });
                _this.getTransferReceiveList();
              }
            });
          }
        },
      });
      // if(this.active==0)
    },
    changeAllCheck() {
      for (let i in this.list) {
        if (this.list[i].status == 1) {
          this.list[i].check = this.allCheck;
        }
      }
    },
    selectItem(index) {
      this.list[index].check = !this.list[index].check;
      let checkList = this.list.filter((item) => item.check);
      if (checkList.length == this.list.length) {
        this.allCheck = true;
      } else {
        this.allCheck = false;
      }
    },
    submit() {
      let list = this.list.filter((item) => item.check) || [];
      if (list.length == 0) {
        this.$vux.toast.show({
          text: "请选择需要接收的资产",
          type: "warn",
        });
      } else {
        let ids = list.map((item) => {
          return item.id;
        });
        AssetApi.transferConfirm({ id: ids.toString() }).then((res) => {
          if (res.code == "0000") {
            this.$vux.toast.show({
              text: "资产接收确认成功",
            });
          }
        });
      }
    },
    // 跳转详情确认接收
    // toDetail(item){
    //     if(item.status == 1){
    //         if(this.active==0){
    //             this.$router.push({
    //                 path: "receiveLend",
    //                 query: {
    //                     checkId: item.checkId,
    //                     alreadyCount: item.alreadyCount,
    //                     unreadCount: item.unreadCount,
    //                 },
    //             });
    //         }else{

    //         }
    //     }
    // }
  },
};
</script>

<style lang="less" scoped>
.receive{
  padding-bottom:20px;
}
.list {
  height: auto;
  background: #f1f1f1;
  padding: 10px;
  margin-bottom: 50px;
}

</style>
